package com.example.demo.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Properties;

@RestController
public class MainController {

    @Value("${myName}")
    private String myName;

    @Autowired
    private FileHandler fileHandler;

    @GetMapping("/getName")
    public String getName() {
        return myName;
    }

    @GetMapping("/setName/{newName}")
    public boolean setName(@PathVariable("newName") String newName) throws IOException {
        System.out.println(newName);
        Properties pro = new Properties();
        pro.setProperty("myName", newName);
        fileHandler.save(pro);
        return true;
    }

    @GetMapping("/calc")
    public String calc(@RequestParam("a") String a, @RequestParam("b") String b) {
        int x, y;
        try {
            x = Integer.parseInt(a);
            y = Integer.parseInt(b);
        } catch (NumberFormatException e) {
            return e.getMessage();
        }

        return String.valueOf(x + y);
    }
}

